package com.airnews.ticket.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;

import com.airnews.ticket.R;
/**
 * 可伸展的列表效果
 * @author  luoxz
 *
 */
public class FlyListViewExpandAdapter extends SimpleExpandableListAdapter  {
	LayoutInflater layoutInflater ;
	List<Map<String, Object>> parentList;
	List<List<Map<String, Object>>> allChildList;
	Context context;

	public FlyListViewExpandAdapter(Context context,List<Map<String, Object>> parentList, List<List<Map<String, Object>>> allChildList) { 
		super(context, parentList, 0, 0, null, null, allChildList, 0, 0, null, null);  
		this.context = context;
		this.parentList = parentList;
		this.allChildList = allChildList;
		this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
	}  
	
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {  
		   ViewHolder holder; 
		   //得到自己定义的List的界面
		  if (convertView == null) { 
			  convertView = this.layoutInflater.inflate(R.layout.fly_row, null); 
			  holder = new ViewHolder(); 
			  holder.airline = (TextView) convertView.findViewById(R.id.airline); 
			  holder.departureTime = (TextView) convertView.findViewById(R.id.departureTime); 
			  holder.flightNo = (TextView) convertView.findViewById(R.id.flightNo); 
			  holder.arrival = (TextView) convertView.findViewById(R.id.arrival); 
			  holder.departure = (TextView) convertView.findViewById(R.id.departure); 
			  holder.airLogo = (ImageView) convertView.findViewById(R.id.airLogo); 
			  holder.baseCabin = (TextView) convertView.findViewById(R.id.baseCabin); 
			  holder.arrivalTime = (TextView) convertView.findViewById(R.id.arrivalTime); 
			  holder.price = (TextView) convertView.findViewById(R.id.price); 
			  holder.basePrice = (TextView) convertView.findViewById(R.id.basePrice); 
			  holder.cabinPrice = (TextView) convertView.findViewById(R.id.cabinPrice); 
			  holder.discount = (TextView) convertView.findViewById(R.id.discount); 
			  holder.seat = (TextView) convertView.findViewById(R.id.seat); 
			  
			  
			  convertView.setTag(holder); 
		  } else {
			  holder = (ViewHolder) convertView.getTag(); 
		  }
		  
		  holder.airline.setText(String.valueOf(this.parentList.get(groupPosition).get("airline")));
		  holder.departureTime.setText(String.valueOf(this.parentList.get(groupPosition).get("departureTime")));
		  holder.flightNo.setText(String.valueOf(this.parentList.get(groupPosition).get("flightNo")));
		  holder.arrival.setText(String.valueOf(this.parentList.get(groupPosition).get("arrival")));
		  holder.departure.setText(String.valueOf(this.parentList.get(groupPosition).get("departure")));
		  holder.basePrice.setText(String.valueOf(this.parentList.get(groupPosition).get("basePrice")));
		  holder.baseCabin.setText(String.valueOf(this.parentList.get(groupPosition).get("baseCabin")));
		  holder.arrivalTime.setText(String.valueOf(this.parentList.get(groupPosition).get("arrivalTime")));
		  holder.price.setText(String.valueOf(this.parentList.get(groupPosition).get("price")));
		  holder.discount.setText(String.valueOf(this.parentList.get(groupPosition).get("discount")));
		  holder.cabinPrice.setText(String.valueOf(this.parentList.get(groupPosition).get("cabinPrice")));
		  holder.airLogo.setImageResource(Integer.valueOf(String.valueOf(this.parentList.get(groupPosition).get("airLogo"))));
		  
		  holder.cabinPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
		  holder.discount.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
		  
		  String seatValue = String.valueOf(this.parentList.get(groupPosition).get("seat"));
		  if(seatValue.indexOf(">9")!=-1) {
			  holder.seat.setVisibility(View.INVISIBLE);
		  } else {
			  holder.seat.setText("票少");
			  holder.seat.setVisibility(View.VISIBLE);
		  }
		  return convertView;
	}
	
    
	@Override  
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {  
		ViewHolder holder; 
		 //得到自己定义的List的界面
		  if (convertView == null) { 
			  convertView = this.layoutInflater.inflate(R.layout.fly_row_more, null); 
			  holder = new ViewHolder(); 
			  //更多的价钱项
			  holder.priceSmall = (TextView) convertView.findViewById(R.id.priceSmall); 
			  holder.basePriceSmall = (TextView) convertView.findViewById(R.id.basePriceSmall); 
			  holder.seatSmall = (TextView) convertView.findViewById(R.id.seatSmall); 
			  holder.baseCabinSmall = (TextView) convertView.findViewById(R.id.baseCabinSmall); 
			  convertView.setTag(holder); 
		  } else {
			  holder = (ViewHolder) convertView.getTag(); 
		  }
		  if(this.allChildList!=null){
			  holder.priceSmall.setText(String.valueOf(this.allChildList.get(groupPosition).get(childPosition).get("price")));
			  holder.basePriceSmall.setText(String.valueOf(this.allChildList.get(groupPosition).get(childPosition).get("basePrice")));
			  holder.baseCabinSmall.setText(String.valueOf(this.allChildList.get(groupPosition).get(childPosition).get("baseCabin")));
			  String seatValue = String.valueOf(this.allChildList.get(groupPosition).get(childPosition).get("seat"));
			  if(seatValue.indexOf(">9")!=-1) {
				  holder.seatSmall.setVisibility(View.INVISIBLE);
			  } else {
				  holder.seatSmall.setText("票少");
				  holder.seatSmall.setVisibility(View.VISIBLE);
			  }
		  }
		  return convertView;
	}  

	static class ViewHolder { 
		TextView airline,departureTime,flightNo; 
		ImageView airLogo;
		TextView arrival,departure,basePrice; 
		TextView baseCabin,arrivalTime,price; 
		TextView cabinPrice,discount,seat; 
		
		TextView basePriceSmall,priceSmall,cabinPriceSmall,baseCabinSmall,seatSmall; 
	} 


}
